package com.wanxi.education.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanxi.education.mapper.UserMapper;
import com.wanxi.education.model.pojo.User;
import com.wanxi.education.model.vo.user.BUserReturnVo;
import com.wanxi.education.model.vo.user.BUserVo;
import com.wanxi.education.model.vo.user.UpdateBUserVo;
import com.wanxi.education.model.vo.user.FindByIdBUserVo;
import com.wanxi.education.service.BUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: YanZhengYi
 * @Description:
 * @Date: Created in 2021/9/24
 * @Version: 1.0
 */
@Service
public class BUserServiceImpl implements BUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public PageInfo<BUserReturnVo> findPage(BUserVo bUserVo) {
        PageHelper.startPage(bUserVo.getPageNum(),bUserVo.getPageSize());
        List<BUserReturnVo> bUserReturnVoList = userMapper.bUserFindPage(bUserVo);
        return new PageInfo<>(bUserReturnVoList);
    }

    @Override
    public Integer update(UpdateBUserVo updateBUserVo) {
        String password = updateBUserVo.getPassword();
        if (password==null){
            User byId = userMapper.findById(updateBUserVo.id);
            updateBUserVo.setPassword(byId.getPassword());
        }
        try {
            updateBUserVo.setPassword(passwordEncoder.encode(updateBUserVo.getPassword()));
            Integer result = userMapper.bUserUpdate(updateBUserVo);
            if (result!=1){
                return null;
            }
            return result;
        }catch (Exception e){return null;}

    }

    @Override
    public Integer delete(Long id) {
        Integer result = userMapper.delete(id);
        if (result!=1){
            return null;
        }
        return result;
    }

    @Override
    @Transactional
    public String insert(UpdateBUserVo updateBUserVo) {
        String phone = updateBUserVo.getPhone();
        User user = userMapper.findByPhoneAndStatusAndId(phone);
        updateBUserVo.setPassword(passwordEncoder.encode(updateBUserVo.getPassword()));
        if (user!=null){
            userMapper.bUserUpdate(updateBUserVo);
        }
        try {
            Integer result = userMapper.bUserInsert(updateBUserVo);
            List<Integer> id = new ArrayList<>();
            id.add(3);
            userMapper.insertRole(id,updateBUserVo.getId());
            if (result!=1){
                return "增加失败";
            }
            return "success";
        }catch (Exception e
        ){
            return "电话重复";
        }

    }

    @Override
    public FindByIdBUserVo findById(Long id) {
        User user = userMapper.findById(id);
        if (user==null)return null;
        //密码不需要回显，需要重新输入
        FindByIdBUserVo findByIdBUserVo = new FindByIdBUserVo();
        BeanUtils.copyProperties(user, findByIdBUserVo);
        return findByIdBUserVo;
    }
}
